package sg.edu.nus.iss.phoenix.authenticate.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sg.edu.nus.iss.phoenix.authenticate.delegate.RoleDelegate;
import sg.edu.nus.iss.phoenix.authenticate.delegate.UserDelegate;
import sg.edu.nus.iss.phoenix.authenticate.entity.Role;
import sg.edu.nus.iss.phoenix.authenticate.entity.User;
import sg.edu.nus.iss.phoenix.core.controller.FCUtilities;

/**
 * Servlet implementation class UserController
 */
@WebServlet("/UserController/*")
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserController() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String selection = FCUtilities.stripPath(request.getPathInfo())
				.toLowerCase();
		RoleDelegate roleDelegate = new RoleDelegate();
		RequestDispatcher rd2;
		UserDelegate userDelegate = new UserDelegate();
		switch (selection) {
		case "load":

			ArrayList<User> data2 = (ArrayList<User>) userDelegate.findAll();
			request.setAttribute("users", data2);
			rd2 = request.getRequestDispatcher("/pages/user/cruduser.jsp");
			rd2.forward(request, response);
			break;
		case "add":

			ArrayList<Role> roles = (ArrayList<Role>) roleDelegate.findAll();
			request.setAttribute("roles", roles);
			rd2 = request.getRequestDispatcher("/pages/user/adduser.jsp");
			rd2.forward(request, response);

			break;
		case "process":
			User user = new User();
			List<Role> selectedRoles = new ArrayList<Role>();
			for (String s : request.getParameterValues("roles")) {
				Role role = new Role();
				role.setRoleId(Integer.parseInt(s));
				selectedRoles.add(role);
			}
			// System.out.println(" adding roles  " + selectedRoles);
			user.setName(request.getParameter("name"));
			user.setFirstName(request.getParameter("firstName"));
			user.setLastName(request.getParameter("lastName"));
			user.setPassword(request.getParameter("password"));
			user.setRoles(selectedRoles);
			String ins = (String) request.getParameter("ins");
			if (ins.equalsIgnoreCase("true")) {
				userDelegate.insert(user);
			} else {
				user.setId(request.getParameter("id"));
				// System.out.println(" updating new user " + user);
				userDelegate.update(user);
			}
			rd2 = request.getRequestDispatcher("/controller/loaduser");
			rd2.forward(request, response);
			break;
		case "delete":
			User user1 = new User();
			user1.setId(request.getParameter("id"));
			User usersession = (User) request.getSession().getAttribute("user");
		
			// System.out.println("session  " + usersession);
			// System.out.println("delete user " + user1);
			if (!usersession.getId().equals(user1.getId())) {
				userDelegate.deleteUser(user1);
				rd2 = request.getRequestDispatcher("/controller/loaduser");
				rd2.forward(request, response);
			}
			break;
		case "edit":
			User user2 = new User();
			user2.setId(request.getParameter("id"));
			user2 = userDelegate.loadUser(user2);
			request.setAttribute("edituser", user2);

			// Add user roles
			Map<Integer, String> r = new HashMap<Integer, String>();
			ArrayList<Role> userRoles = new ArrayList<Role>();
			for (Role rol : user2.getRoles()) {
				r.put(rol.getRoleId(), rol.getRoleName());
			}
			request.setAttribute("userroles", user2.getRoles());

			// Adding all existing roles in the site
			ArrayList<Role> allRoles = (ArrayList<Role>) roleDelegate.findAll();
			request.setAttribute("allroles", allRoles);

			request.setAttribute("insert", "false");

			rd2 = request.getRequestDispatcher("/pages/user/setupuser.jsp");
			rd2.forward(request, response);
			break;
		case "select":
			String type = request.getParameter("type");
			ArrayList<User> data3 = (ArrayList<User>) userDelegate.findAll();
			request.setAttribute("users", data3);
			request.setAttribute("type", type);
			rd2 = request.getRequestDispatcher("/pages/user/selectuser.jsp");
			rd2.forward(request, response);
			break;

		}
	}

}
